#include <iostream>
#include <cstring>
#include <algorithm>

class myString {
private:
    char* data; // 存储字符串数据的指针
    size_t length; // 字符串长度

public:
    // 默认构造函数
    myString() : data(nullptr), length(0) {
        std::cout << "Default Constructor called.\n";
    }

    // 带参构造函数 (char*)
    myString(const char* s) {
        std::cout << "Parameterized Constructor (char*) called.\n";
        if (s) {
            length = strlen(s);
            data = new char[length + 1];
            strcpy(data, s);
        } else {
            data = nullptr;
            length = 0;
        }
    }

    // 析构函数
    ~myString() {
        std::cout << "Destructor called for: ";
        if (data) {
            std::cout << data;
            delete[] data;
            data = nullptr;
        }
        std::cout << "\n";
    }

    // 拷贝构造函数 (深拷贝)
    myString(const myString& other) {
        std::cout << "Copy Constructor called for: " << (other.data ? other.data : "(empty)") << "\n";
        length = other.length;
        if (other.data) {
            data = new char[length + 1];
            strcpy(data, other.data);
        } else {
            data = nullptr;
        }
    }

    // 拷贝赋值运算符 (深拷贝，提供强异常安全保证)
    myString& operator=(const myString& other) {
        std::cout << "Copy Assignment Operator called for: " << (other.data ? other.data : "(empty)") << "\n";
        if (this != &other) { // 防止自我赋值
            // 创建临时对象，利用其构造函数进行深拷贝
            myString temp(other);
            // 交换资源，利用临时对象的析构函数释放旧资源
            std::swap(data, temp.data);
            std::swap(length, temp.length);
        }
        return *this;
    }

    // 移动构造函数 (C++11, 浅拷贝，接管资源)
    myString(myString&& other) noexcept {
        std::cout << "Move Constructor called for: " << (other.data ? other.data : "(empty)") << "\n";
        data = other.data;
        length = other.length;
        other.data = nullptr; // 将源对象置空，防止二次释放
        other.length = 0;
    }

    // 移动赋值运算符 (C++11, 浅拷贝，接管资源)
    myString& operator=(myString&& other) noexcept {
        std::cout << "Move Assignment Operator called for: " << (other.data ? other.data : "(empty)") << "\n";
        if (this != &other) { // 防止自我赋值
            delete[] data; // 释放当前对象的资源
            data = other.data;
            length = other.length;
            other.data = nullptr; // 将源对象置空
            other.length = 0;
        }
        return *this;
    }

    // 获取字符串长度
    size_t size() const { return length; }

    // 获取C风格字符串
    const char* c_str() const { return data ? data : ""; }

    // 打印字符串
    void print() const {
        std::cout << (data ? data : "(empty)") << " (Length: " << length << ")\n";
    }
};

int main() {
    std::cout << "--- Test Default Constructor ---\n";
    myString s0;
    s0.print();

    std::cout << "\n--- Test Parameterized Constructor ---\n";
    myString s1("Hello");
    s1.print();

    std::cout << "\n--- Test Copy Constructor ---\n";
    myString s2 = s1; // 调用拷贝构造函数
    s2.print();

    std::cout << "\n--- Test Copy Assignment Operator ---\n";
    myString s3;
    s3 = s1; // 调用拷贝赋值运算符
    s3.print();

    std::cout << "\n--- Test Move Constructor ---\n";
    myString s4 = std::move(s1); // 调用移动构造函数，s1资源被转移
    s4.print();
    s1.print(); // s1现在应该是空的

    std::cout << "\n--- Test Move Assignment Operator ---\n";
    myString s5("World");
    s5 = std::move(s2); // 调用移动赋值运算符，s2资源被转移
    s5.print();
    s2.print(); // s2现在应该是空的

    std::cout << "\n--- End of Main ---\n";
    return 0;
}